package com.sanjiahantang.youjiarole.module.complain.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.FileBean;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.models.IFileModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.FileModel;
import com.sanjiahantang.youjiarole.module.complain.beans.ComplainBean;
import com.sanjiahantang.youjiarole.module.complain.models.IComplainModel;
import com.sanjiahantang.youjiarole.module.complain.models.impl.ComplainModel;
import com.sanjiahantang.youjiarole.module.complain.views.ComplainView;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zy on 2017/9/11.
 */

public class ComplainPresenter extends BaseMvpPresenter<ComplainView> {

    private QuickAdapter<ComplainBean.Image> adapter;
    private List<ComplainBean.Image> data;
    private IComplainModel complainModel;
    private IFileModel fileModel;

    public ComplainPresenter() {
        data = new ArrayList<>();
        complainModel = new ComplainModel();
        fileModel = new FileModel();
    }
    private final static String ADD = "add";
    private ArrayList<String> mResults = new ArrayList<>();
    public QuickAdapter getAdapter(Context context) {
        if(adapter == null){
            adapter = new QuickAdapter<ComplainBean.Image>(context, R.layout.item_image) {
                @Override
                protected void convert(BaseAdapterHelper helper, ComplainBean.Image item) {
                    ImageView iv = helper.getView(R.id.image);
                    if (ADD.equals(item.getId())){

                    }else {
                        Picasso.with(context).load(item.getUrl()).into(iv);
                    }

                }

                @Override
                public ComplainBean.Image getItem(int position) {
                    if (position < this.getCount()){
                        return super.getItem(position);
                    }else {
                        ComplainBean.Image add = new ComplainBean.Image();
                        add.setId(ADD);
                        return add;
                    }
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    if(position == getCount() - 1){
                        convertView = LayoutInflater.from(context).inflate(R.layout.item_add_image,null);
                        convertView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            }
                        });
                        return convertView;
                    }
                    return super.getView(position, convertView, parent);
                }

                @Override
                public int getCount() {
                    return super.getCount() + 1;
                }
            };
        }

        return adapter;
    }

    public void onActivityResult(Context context, int requestCode, int resultCode, Intent data) {
        if (requestCode == ComplainView.REQUEST_CODE){

        }
    }

    public void addRoleComplaint(final Context context,
                                 String ccid,
                                 String complaintLoginId,
                                 String complaintName,
                                 String complaintObjectLoginId,
                                 String complaintObjectName,
                                 String complaintRole,
                                 String projectId,
                                 String complaintContent) {
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(ComplainView.KEY_COMPLAIN_CCID, ccid);
        param.put(ComplainView.KEY_COMPLAIN_LOGINID, complaintLoginId);
        param.put(ComplainView.KEY_COMPLAIN_NAME, complaintName);
        param.put(ComplainView.KEY_COMPLAIN_OBJECT_LOGINID, complaintObjectLoginId);
        param.put(ComplainView.KEY_COMPLAIN_OBJECT_NAME, complaintObjectName);
        param.put(ComplainView.KEY_COMPLAIN_ROLE, complaintRole);
        param.put(ComplainView.KEY_COMPLAIN_PROJECTID, projectId);
        param.put(ComplainView.KEY_COMPLAIN_CONTENT, complaintContent);
        String nowPhone = SPUtils.get(context, SPConfig.PHONE,"").toString();
        if (!nowPhone.equals(complaintLoginId)){
            param.put("subcontractPhone", nowPhone);
        }
        mView.showProgress();
        complainModel.addRoleComplaint(context, param, new CallBack<ComplainBean>() {
            @Override
            public void onSuccess(ComplainBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    ((Activity) context).finish();
                    Utils.showToast(context, "投诉成功！");
                }else if (response != null && !TextUtils.isEmpty(response.getMessage())){
                    Utils.showToast(context,response.getMessage());
                }else{
                    Utils.showToast(context,"投诉失败");
                    ((Activity)context).finish();
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context, message);
            }
        });
    }

    public void doUploadComplainPic(Context context, Uri uri, int id, final BaseView.OnLoadListener listener) {
        Bitmap bitmap = null;
        try {
            bitmap = Utils.getBitmapFormUri((Activity) context, uri);
            doUploadComplainPic(context,bitmap,id,listener);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doUploadComplainPic(Context context, Bitmap bitmap, int id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("fileId", "0");
        param.put("busiId", String.valueOf(id));
        param.put("busiTypeId", "complaint");
        param.put("fileName", System.currentTimeMillis() + ".jpg");
        param.put(KEY_FILE_BASE64_PIC, Utils.bitmapToBase64(bitmap));

        mView.showProgress();
        fileModel.doUpoad(context, param, new CallBack<FileBean>() {
            @Override
            public void onSuccess(FileBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }

    public void deleteImage(Context context, final int id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("fileId", String.valueOf(id));
        mView.showProgress();
        fileModel.deleteFileRole(context, param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(id);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }

    public void cardSelect(Activity context) {
        Intent it = new Intent(Intent.ACTION_PICK);
        it.setType("image/*");
        context.startActivityForResult(it, INTENT_SELECT_PIC);
    }

    public void findMaxId(Context context, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        mView.showProgress();
        complainModel.findMaxId(context, param, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    int id = Integer.valueOf(response.getData());
                    listener.onLoad(id);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }
}
